﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_Emp_Study : AdminPageBase
{
   
    private string returnURL;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        if (!IsPostBack)
        {

            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "PlanFinishId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");            
            GridViewBind();
           

        }


    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void GridViewBind()
    {

        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];
        if (AdminSessionHelper.UserDetails != null)
        {
            DataSet ds = jtDataAccess.GetPlanFinishByUserId(Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString()));
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据

                GridviewControl.GridViewDataBind(this.GridView1, ds.Tables[0]);

                // this.GridView1.DataSource = ds.Tables[0];
                // this.GridView1.DataBind();
                //pager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("PlanFinishId");
                dt.Columns.Add("ClassId");
                dt.Columns.Add("Code");
                dt.Columns.Add("ClassTypeId");
                dt.Columns.Add("ClassTypeName");
                dt.Columns.Add("Name");
                dt.Columns.Add("Time");
                dt.Columns.Add("IsValidDesc");
                dt.Columns.Add("IsValid");
                dt.Columns.Add("Score");
                dt.Columns.Add("Finish");
                dt.Columns.Add("Period");
                dt.Columns.Add("CanStudy");
                dt.Columns.Add("CanExam");
                dt.Columns.Add("StudyDate", typeof(DateTime));
                dt.Columns.Add("FinishDate", typeof(DateTime));
                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView1, dt);
                // pager1.RecordCount = 0;
            }
        }
        else
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);


    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            e.Row.Cells[0].Attributes.Add("class", "text");
            Label lblstartdate = (Label)e.Row.FindControl("lblStudyDate");
            Label lblenddate = (Label)e.Row.FindControl("lblFinishDate");
            Label lbScore = (Label)e.Row.FindControl("lblScore");
            Button lbtExam = (Button)e.Row.FindControl("lbtExam");
            HiddenField hdfCanExam = (HiddenField)e.Row.FindControl("hdfCanExam");
            if (lblstartdate != null && lblstartdate.Text != "")
            {
                lblstartdate.Text = Convert.ToDateTime(lblstartdate.Text).ToShortDateString().Replace("1900/1/1", "");
            }
            if (lblenddate != null && lblenddate.Text != "")
            {
                lblenddate.Text = Convert.ToDateTime(lblenddate.Text).ToShortDateString().Replace("1900/1/1", "").Replace("2099/12/31", "");
            }
            if (lbScore != null && lbScore.Text.Equals("0.00"))
                lbScore.Text = "";
            //if (lbtExam != null)
            //{
            //    if (hdfCanExam != null && hdfCanExam.Value.Equals("1"))
            //    {
            //        lbtExam.Visible=true;
            //    }
            //    else
            //        lbtExam.Visible = false;
            //}
        }

    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.GridViewBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "learn")
        {
            int canexam = 0;
            int canstudy = 0;
            GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
            HiddenField hdfCanExam = (HiddenField)drv.FindControl("hdfCanExam");
            HiddenField hdfCanStudy = (HiddenField)drv.FindControl("hdfCanStudy");
            if (hdfCanExam != null && hdfCanExam.Value != "")
                canexam = Convert.ToInt32(hdfCanExam.Value);
            if (hdfCanStudy != null && hdfCanStudy.Value != "")
                canstudy = Convert.ToInt32(hdfCanStudy.Value);
            returnURL = "Study.aspx?MenuId=" + MenuId;
            //Response.Redirect("StudyDetail.aspx?id=" + e.CommandArgument.ToString()+"&CanStudy=" +canstudy+"&CanExam="+canexam+ "&returnURL=" + Server.UrlEncode(returnURL));
           string url="learn.aspx?id=" + e.CommandArgument.ToString() + "&CanStudy=" + canstudy + "&CanExam=" + canexam + "&returnURL=" + Server.UrlEncode(returnURL);
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('"+url+"'); </script>",false);
           
        }
        else if (e.CommandName == "exam")
        {
           
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            returnURL = "Study.aspx?MenuId=" + MenuId;
            //Response.Redirect("ExamDetail.aspx?id=" + e.CommandArgument.ToString() + "&returnURL=" + Server.UrlEncode(returnURL));
            string url = "examination.aspx?id=" + e.CommandArgument.ToString()+ "&returnURL=" + Server.UrlEncode(returnURL);
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('" + url + "'); </script>", false);
        }
    }
   

}